package ${rootPackage}.actions;

import java.util.List;

import ${rootPackage}.models.${modelClassName};
import ${rootPackage}.services.${modelClassName}Service;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Scope;

@Component
@Scope("prototype")
@Results({
    @Result(name="success", type="redirect", location="${actionName}")
})
public class ${actionClassName} implements Preparable {
    private ${modelClassName} model = new ${modelClassName}();
    private Long id;
    private List<${modelClassName}> list;
    private ${modelClassName}Service service;

    // GET /${modelName}/1
    public HttpHeaders show() {
        return new DefaultHttpHeaders("show");
    }

    // GET /${modelName}
    public HttpHeaders index() {
        list = service.getAll();
        return new DefaultHttpHeaders("index").disableCaching();
    }

    // GET /${modelName}/1/edit
    public String edit() {
        return "edit";
    }

    // GET /${modelName}/new
    public String editNew() {
        model = new ${modelClassName}();
        return "editNew";
    }

    // DELETE /${modelName}/1
    public String destroy() {
        service.remove(id);
        return Action.SUCCESS;
    }

    // POST /${modelName}
    public HttpHeaders create() {
        service.save(model);
        return new DefaultHttpHeaders(Action.SUCCESS).setLocationId(model.getId());
    }

    // PUT /${modelName}/1
    public String update() {
        service.save(model);
        return "success";
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void prepare() throws Exception {
        if (id != null)
            model = service.get(id);
    }

    public ${modelClassName} getModel() {
        return model;
    }

    public List<${modelClassName}> getModelList() {
        return list;
    }

    //inject service
    public void set${modelClassName}Service(${modelClassName}Service service) {
        this.service = service;
    }
}
